#!/bin/bash
# --
# Chris Baird,, <vk2cjb@gmail.com> October 2020
# --
# If you need instructions for adapting this, you're
# in the wrong neighborhood, oniichan.

REC=/tmp/rec.wav
TTSWAV=/tmp/tts.wav
WEBSTORE=/home/cjb/www/Parrot_Archive
MULTIMONPROG="multimon-ng"

FREQ=146575000
MODE=PKTFM
KEYMODE=1

########################################

radiosetup() {
    rigctl -m 120 -r /dev/ttyUSB0 -s 38400 F $FREQ M $MODE 0
    a=`rigctl -m 120 -r /dev/ttyUSB0 -s 38400 f`
    if [ "X"$a != "X"$FREQ ]; then echo "Couldn't set radio"; exit; fi
    echo -n "Press Enter if radio looks okay..."
    read a
}


ptt() {
    if [ "X"$1 == "Xon" ]
    then 
	echo -n "Keying transmitter"
	rigctl -m 120 -r /dev/ttyUSB0 -s 38400 T $KEYMODE
	sleep 0.5
	echo
    else
	echo -n "Unkeying transmitter"
	sleep 0.5
	rigctl -m 120 -r /dev/ttyUSB0 -s 38400 T 0
	echo
    fi
}


audioradio() {
    echo -n "Transmitting audio"
    mplayer -ao alsa:device=hw=CODEC $1 >/dev/null 2>/dev/null
    #mplayer -quiet >/dev/null 2>/dev/null $1
    echo
}


tts() {
    rm -f $TTSWAV
    espeak -v mb/mb-de3-en -p 0 -s 120 -g 1 -w $TTSWAV "$1"
    audioradio $TTSWAV
}


####################
# copypasted from Phillip J Rhoades' 'simplex repeater' script

scandtmf() {
    dtmfactions $($MULTIMONPROG -q -t wav -a DTMF $REC 2>/dev/null |\
	 grep "DTMF" |grep -v "Enabled" |sed 's/^.\{6\}//'|tr -d '\n')
}


dtmfactions() {
    if [ $# -eq 1 ]
    then
        echo " Received DTMF Command: $1"
	tts "Received D T M F command number $1"
	
        if [ $1 = "1" ]; then
            tts "The date and time. `date +"%A, %B %d, %I:%M %p"`"
            return
        fi
	
        if [ $1 = "2" ]; then
            tts "A strange game. The only winning move is not to play."
            return
        fi
	
        if [ $1 = "300" ]; then
            tts "300 baud test transmission."
	    rm -f /tmp/300.wav
	    head -50 wia.news.txt | minimodem --tx 300 -f /tmp/300.wav
	    audioradio /tmp/300.wav
	    sleep 1
	    tts "VK2CJB Parrot Repeater."
            return
        fi
	
        if [ $1 = "1200" ]; then
            tts "1200 baud test transmission."
	    rm -f /tmp/1200.wav
	    head -200 wia.news.txt | minimodem --tx 1200 -f /tmp/1200.wav
	    audioradio /tmp/1200.wav
	    sleep 1
	    tts "VK2CJB Parrot Repeater."
            return
        fi
	
        if [ $1 = "13" ]; then
            tts "Morse test transmission."
	    rm -f /tmp/morse.wav
	    tts "Sorry not implimented yet."
            return
        fi
	
        if [ $1 = "18" ]; then
            tts "R T T Y test transmission."
	    rm -f /tmp/rtty.wav
	    cat rtty.txt | minimodem --tx rtty -f /tmp/rtty.wav
	    audioradio /tmp/rtty.wav
	    sleep 1
	    tts "VK2CJB Parrot Repeater."
            return
        fi
	
        if [ $1 = "19" ]; then
            tts "S S T V test transmission."
	    rm -f /tmp/sstv.wav
	    bash ./sstv_beacon.sh /tmp/sstv.wav
	    audioradio /tmp/sstv.wav
	    sleep 1
	    tts "VK2CJB Parrot Repeater."
            return
        fi
	
        if [ $1 = "20" ]; then
            tts "S S T V test transmission."
	    f=`ls -tr1 images/*|head -1`
	    echo "Encoding " $f
	    rm -f /tmp/sstv.wav
	    python -m pysstv --mode ScottieS1 --keep-aspect-ratio --resize \
		   --resample bicubic $f /tmp/sstv.wav
	    touch $f
	    audioradio /tmp/sstv.wav
	    sleep 1
	    tts "VK2CJB Parrot Repeater."
            return
        fi
	
    fi
}

# 6 - FAQ
# 8 - Hamming
# 22 - "V" visual spectrogram/SSTV of the over

####################

radiosetup

while true
do
	sync
	captureaudio >$REC -t 300 -s 5 -k 5 -d "hw:CARD=CODEC,DEV=0"
	sync
	
	d=`date "+%F-%R:%S"`
	echo
	echo $d
	ls -l $REC 2>/dev/null

	ptt on
	tts "VK2CJB Parrot Repeater."

	if [ -s $REC ]
	then
	    # try decoding AFSK/RTTY..
	    
	    tts "Play-back starts."
	    audioradio $REC
	    tts "Play-back ends."
	    scandtmf

	    ffmpeg -i $REC -b:a 64k -ac 1 $WEBSTORE/${d}.mp3 >/dev/null 2>/dev/null
	    chmod 644 $WEBSTORE/${d}.mp3
	    ls -l $WEBSTORE/${d}.mp3
	    rm -f $REC
	    sync
	fi
	
	tts "Recording."
	ptt off
done

